<div class="tmd-doc">
<p></p>
<h1 class="tmd-header-1">
operator 3
</h1>
<p></p>
<div id="question" class="tmd-base">
<p></p>
<div class="tmd-usual">
What does the following program print?
</div>
<p></p>
<pre class="tmd-code line-numbers">
<code class="language-go">package main

const N = 1
var n = N
var a byte = 128 &lt;&lt; N &gt;&gt; N
var b byte = 128 &lt;&lt; n &gt;&gt; n

func main() {
	println(a, b)
}
</code></pre>
</div>
<p></p>
<div class="tmd-usual">
Choices:
</div>
<p></p>
<input type="radio" id="choiceA" name="choice" class="choice">
<input type="radio" id="choiceB" name="choice" class="choice">
<input type="radio" id="choiceC" name="choice" class="choice">
<input type="radio" id="choiceD" name="choice" class="choice">

<div id="choices">

<ul style="list-style-type:none;">
<li><label for="choiceA">128 128</label></li>
<li><label for="choiceB">128 0</label></li>
<li><label for="choiceC">0 128</label></li>
<li><label for="choiceD">0 0</label></li>
</ul>

</div><p></p>
<div id="answer" class="tmd-base">
<div class="tmd-usual">
Answer: 128 0
</div>
<p></p>
<div class="tmd-usual">
Run it on <a href="https://go.dev/play/p/cam2cOt7UnS">Go play</a>.
</div>
<p></p>
<div class="tmd-usual">
Key points:
</div>
<ul class="tmd-list">
<li class="tmd-list-item">
<div class="tmd-usual">
If the operands in an operator expression are both/all constants, then the expression is evaluated at compile time. In the above program, <code class="tmd-code-span">128 &lt;&lt; N &gt;&gt; N</code> is such an expression. In this expression, <code class="tmd-code-span">128</code> is deduced as an untyped <code class="tmd-code-span">int</code> value.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
In a bit-shift expression, if the left operand is an untyped constant and the right operand is not constant, then <a href="https://go101.org/article/operators.html#bitwise-shift-left-operand-type-deduction">the type of the left operand will be deduced as the final assumed type</a>. In the above program, <code class="tmd-code-span">128 &lt;&lt; n &gt;&gt; n</code> is a such expression. In this expression, the type of <code class="tmd-code-span">128</code> is deduced as the assumed type, <code class="tmd-code-span">byte</code>. <code class="tmd-code-span">128 &lt;&lt; n</code> overflows the value range of <code class="tmd-code-span">byte</code>, so it is truncated to 0.
</div>
</li>
</ul>
<p></p>
<div class="tmd-usual">
This is one small detail in Go programming. Have interests on more ones? Please read <a href="https://go101.org/details-and-tips/101.html">Go Details &amp; Tips 101</a> book.
</div>
<p></p>
</div>
<p></p>
</div>
